//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004 Siemens AG
//
// Version: 1.1.2.5
// Date:    11. Jan. 2006
//////////////////////////////////////////////////////////////////////////////
//
//
// (C) Copyright 2007 Marvell International Ltd.
//  All Rights Reserved
//

#include "precomp.h"

//////////////////////////////////////////////////////////////////////////////
//
// This file contains all arrays with the AT commands for module
// initialization.
// The first entry in each array corresponding to MODEMTYPE_UNKNOWN defines
// the default initialization. Use NULL instead of initialization strings for
// modules where the default intialization shall be used.
//
// Attention:
// The order of entries in all arrays must fit the "MODEMTYPE" enum!
//
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
// Some defines
//
//////////////////////////////////////////////////////////////////////////////

#define INVALID_PORT_FKT            -1
#define NUMBER_OF_PORT_FKTS         (sizeof(g_dwPortCaps) / sizeof(g_dwPortCaps[0]))
#define NUMBER_OF_PREINIT_ENTRIES   2
#define NUMBER_OF_PREINIT_CMDS      (sizeof(g_szPreInitString) / sizeof(g_szPreInitString[0]))

typedef const INITSTRING_DATA PortInitData[NUMBER_OF_SUPPORTED_MODEMTYPES][NUMBER_OF_PREINIT_ENTRIES];

//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
//  A v a i l a b e    p o r t    c a p a b i l i t i e s
//
//////////////////////////////////////////////////////////////////////////////
// Attention:
// This array assigns the supported port function types to their capabilities.
// The assignments made in this array must match exactly the order of entries
// in the following initialization arrays!
//////////////////////////////////////////////////////////////////////////////

const DWORD g_dwPortCaps[] = {
  PORT_CAPS_CMD,                    // Command port
  PORT_CAPS_URC,                    // URC port
  PORT_CAPS_CMD | PORT_CAPS_URC,    // Command and URC port
  PORT_CAPS_GPRS | PORT_CAPS_CSD,   // Data port
  PORT_CAPS_GPRS | PORT_CAPS_RSA,   // Auxiliar Data port (GPRS and with some modules RSA)
};

//////////////////////////////////////////////////////////////////////////////
//
//  V e r y    f i r s t    p o r t    i n t i a l i z a t i o n
//
//////////////////////////////////////////////////////////////////////////////

/* Please notices: it needn't send serial port initialization cmd now. It had been done in MUX driver */
#ifdef RIL_ENABLE_RING_ON_INCOMMING_DATA
//const char* g_szPreInitString[] = { "ATE0V1Q0+CMEE=0\r", "AT&S0\r", "AT\\Q3\r", "AT+CSCS=\"GSM\"\r", "AT^SCFG=\"URC/Ringline\",\"asc0\"\r", "AT^SCFG=\"GPRS/RingOnIncomingData\",\"on\"\r", "AT+CSCS=\"UCS2\"\r" };
const char* g_szPreInitString[] = { "ATE0V1Q0+CMEE=0\r", "AT+CSCS=\"GSM\"\r", "AT^SCFG=\"URC/Ringline\",\"asc0\"\r", "AT^SCFG=\"GPRS/RingOnIncomingData\",\"on\"\r", "AT+CSCS=\"UCS2\"\r" };
#else
//const char* g_szPreInitString[] = { "ATE0V1Q0+CMEE=0\r", "AT&S0\r", "AT\\Q3\r", "AT+CSCS=\"UCS2\"\r" };

#ifdef RIL_SINGLE_PORT
const char* g_szPreInitString[] = {"ATE0V1Q0+CMEE=0\r", "AT\\Q3\r", "AT+CSCS=\"UCS2\"\r" };
#else
const char* g_szPreInitString[] = {"ATE0V1Q0+CMEE=0\r", "AT+CSCS=\"UCS2\"\r" };
#endif

#endif

//////////////////////////////////////////////////////////////////////////////
//
//  P r e    -    P I N    i n t i a l i z a t i o n
//
//////////////////////////////////////////////////////////////////////////////

PortInitData g_rgisdModuleInit[NUMBER_OF_PORT_FKTS] = {

//////////////////////////////////////////////////////////////////////////////
// Pre-PIN intitialization for command only port
//////////////////////////////////////////////////////////////////////////////
  {
    { // Default initialization
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0", CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of Default initialization
    { // MC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC45 initialization
    { // MC46 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC46 initialization
    { // MC55 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC55 initialization
    { // MC56 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC56 initialization
    { // AC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of AC45 initialization
    { // MC75 initialization
      // We have to set the ring line onfiguration of this port as well since an URC during the execution
      // of a command is treated like an URC during data mode.
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of MC75 initialization
    { // TC63 initialization
      // We have to set the ring line onfiguration of this port as well since an URC during the execution
      // of a command is treated like an URC during data mode.
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of TC63 initialization
  },
//////////////////////////////////////////////////////////////////////////////
// End of pre-PIN intitialization for command only port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Pre-PIN intitialization for URC only port
//////////////////////////////////////////////////////////////////////////////
  {
    { // Default initialization
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=2|+CLIP=1|+CMGF=0|^SCKS=1|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of Default initialization
    { // MC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC45 initialization
    { // MC46 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC46 initialization
    { // MC55 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC55 initialization
    { // MC56 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC56 initialization
    { // AC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of AC45 initialization
    { // MC75 initialization
      // We have to set the ring line onfiguration of this port as well since an URC during the execution
      // of a command is treated like an URC during data mode.
      // MC75 V2.001: we don't use CLIP, instead we rely on ^SLCC
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=0;+CREG=2|+CLIP=0|+CMGF=0|^SCKS=1|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of MC75 initialization
    { // TC63 initialization
      // We have to set the ring line onfiguration of this port as well since an URC during the execution
      // of a command is treated like an URC during data mode.
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=2|+CLIP=1|+CMGF=0|^SCKS=1|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of TC63 initialization
  },
//////////////////////////////////////////////////////////////////////////////
// End of pre-PIN intitialization for URC only port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Pre-PIN intitialization for command and URC port
//////////////////////////////////////////////////////////////////////////////
  {
    { // Default initialization
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=2|+CLIP=1|+CMGF=0|^SCKS=1|^SM20=0,0|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of Default initialization
    { // MC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC45 initialization
    { // MC46 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC46 initialization
    { // MC55 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC55 initialization
    { // MC56 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC56 initialization
    { // AC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of AC45 initialization
    { // MC75 initialization
      // We have to set the ring line onfiguration of this port as well since an URC during the execution
      // of a command is treated like an URC during data mode.
      // MC75 V2.001: we don't use CLIP, instead we rely on ^SLCC
#ifndef RIL_SINGLE_PORT
      { "E0V1|\\Q3|&C0&D0&S0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=2|+CLIP=0|+CMGF=0|^SCKS=1|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
#else
      { "E0V1|\\Q3|&C1&D2&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=2|+CLIP=0|+CMGF=0|^SCKS=1|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
#endif


    }, // End of MC75 initialization
    { // TC63 initialization
      // We have to set the ring line onfiguration of this port as well since an URC during the execution
      // of a command is treated like an URC during data mode.
      { "E0V1|\\Q3|&D0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=2|+CLIP=1|+CMGF=0|^SCKS=1|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"|" CMD_INDICATOR_CTRL, CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of TC63 initialization
  },
//////////////////////////////////////////////////////////////////////////////
// End of pre-PIN intitialization for command and URC port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Pre-PIN intitialization for data port
//////////////////////////////////////////////////////////////////////////////
  {
    { // Default initialization
      { "E0V1|\\Q3|&C1&D1&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=0|+CLIP=0|+CMGF=0|+FCLASS=0|^SM20=0,0", CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of Default initialization
    { // MC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC45 initialization
    { // MC46 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC46 initialization
    { // MC55 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC55 initialization
    { // MC56 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC56 initialization
    { // AC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of AC45 initialization
    { // MC75 initialization
#ifndef RIL_SINGLE_PORT
      { "E0V1|\\Q3|&C0&D0&S0|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=0|+CLIP=0|+CMGF=0|+FCLASS=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
#else
      { "E0V1|\\Q3|&C1&D1&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=0|+CLIP=0|+CMGF=0|+FCLASS=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},      
#endif

    }, // End of MC75 initialization
    { // TC63 initialization
      { "E0V1|\\Q3|&C1&D1&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=1;+CREG=0|+CLIP=0|+CMGF=0|+FCLASS=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of TC63 initialization
  },
//////////////////////////////////////////////////////////////////////////////
// End of pre-PIN intitialization for data port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Pre-PIN intitialization for auxilliar data port
//////////////////////////////////////////////////////////////////////////////
  {
    { // Default initialization
      { "E0V1|\\Q3|&C1&D1&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0", CMDOPT_INIT | CMDOPT_GENERALINIT},
    }, // End of Default initialization
    { // MC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC45 initialization
    { // MC46 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC46 initialization
    { // MC55 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC55 initialization
    { // MC56 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of MC56 initialization
    { // AC45 initialization
      { NULL, 0 },
      { NULL, 0 },
    }, // End of AC45 initialization
    { // MC75 initialization
      { "E0V1|\\Q3|&C1&D1&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
#ifdef RIL_ENABLE_RING_ON_INCOMMING_DATA
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0|^SCFG=\"URC/Ringline\",\"0061007300630030\"|^SCFG=\"GPRS/RingOnIncomingData\",\"006F006E\"|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
#else
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
#endif
    }, // End of MC75 initialization
    { // TC63 initialization
      { "E0V1|\\Q3|&C1&D1&S1|Q0X4|+CSCS=\"UCS2\"", CMDOPT_INIT},
#ifdef RIL_ENABLE_RING_ON_INCOMMING_DATA
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0|^SCFG=\"URC/Ringline\",\"0061007300630030\"|^SCFG=\"GPRS/RingOnIncomingData\",\"006F006E\"|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
#else
      { "+CMEE=1|+CRC=0;+CREG=0|+CLIP=0|+CMGF=0|^SM20=0,0|^SCFG=\"URC/Datamode/Ringline\",\"006F006E\"", CMDOPT_INIT | CMDOPT_GENERALINIT},
#endif
    }, // End of TC63 initialization
  },
//////////////////////////////////////////////////////////////////////////////
// End of pre-PIN intitialization for auxilliar data port
//////////////////////////////////////////////////////////////////////////////
};

//////////////////////////////////////////////////////////////////////////////
//
//  P o s t    -    P I N    i n t i a l i z a t i o n
//
//////////////////////////////////////////////////////////////////////////////
// Attention:
//   The USSD URC (AT+CUSD=1) has to be used on the command port only. If
//   this would be used on the Aux or Data port, an incomming URC would
//   block.
// Attention:
//   The COLP URC (AT+COLP=1) has to be used on the same port as the ATD
//   commands for the voice calls.
//////////////////////////////////////////////////////////////////////////////

const char* g_szUnlockedInitString[NUMBER_OF_PORT_FKTS][NUMBER_OF_SUPPORTED_MODEMTYPES] = {

//////////////////////////////////////////////////////////////////////////////
// Post-PIN intitialization for command only port
//////////////////////////////////////////////////////////////////////////////
  {
    // Default initialization
    "AT+CR=0;+CGREG=0;+COLP=1;+CCWA=0;+CUSD=1\r",
    // MC45 initialization
    "AT+CR=0;+CGREG=0;+CCWA=0;+CUSD=1\r",
    // MC46 initialization
    "AT+CR=0;+CGREG=0;+CCWA=0;+CUSD=1\r",
    // MC55 initialization
    NULL,
    // MC56 initialization
    NULL,
    // AC45 initialization
    NULL,
    // MC75 initialization
    NULL,
    // TC63 initialization
    NULL,
  },
//////////////////////////////////////////////////////////////////////////////
// End of post-PIN intitialization for command only port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Post-PIN intitialization for URC only port
//////////////////////////////////////////////////////////////////////////////
  {
    // Default initialization
    "AT+CR=1;+CGREG=1;+COLP=0;+CCWA=1;+CMER=2,0,0,2;^SLCC=1\r",
    // MC45 initialization
    "AT+CR=1;+CGREG=1;+CCWA=1;+CMER=2,0,0,2;^SLCC=1\r",
    // MC46 initialization
    "AT+CR=1;+CGREG=1;+CCWA=1;+CMER=2,0,0,2;^SLCC=1\r",
    // MC55 initialization
    NULL,
    // MC56 initialization
    NULL,
    // AC45 initialization
    NULL,
    // MC75 initialization
    "AT+CR=1;+CGREG=1;+COLP=0;+CCWA=1;^SLCC=1\r",
    // TC63 initialization
    "AT+CR=1;+CGREG=1;+COLP=0;+CCWA=1;^SLCC=1\r",
  },
//////////////////////////////////////////////////////////////////////////////
// End of post-PIN intitialization for URC only port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Post-PIN intitialization for command and URC port
//////////////////////////////////////////////////////////////////////////////
  {
    // Default initialization
    "AT+CR=1;+CGREG=1;+COLP=1;+CCWA=1;+CMER=2,0,0,2;+CUSD=1;^SLCC=1\r",
    // MC45 initialization
    "AT+CR=1;+CGREG=1;+CCWA=1;+CMER=2,0,0,2;+CUSD=1;^SLCC=1\r",
    // MC46 initialization
    "AT+CR=1;+CGREG=1;+CCWA=1;+CMER=2,0,0,2;+CUSD=1;^SLCC=1\r",
    // MC55 initialization
    NULL,
    // MC56 initialization
    NULL,
    // AC45 initialization
    NULL,
    // MC75 initialization
    "AT+CR=1;+CGREG=1;+COLP=1;+CCWA=1;+CUSD=1;^SLCC=1\r",
    // TC63 initialization
    "AT+CR=1;+CGREG=1;+COLP=1;+CCWA=1;+CUSD=1;^SLCC=1\r",
  },
//////////////////////////////////////////////////////////////////////////////
// End of post-PIN intitialization for command and URC port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Post-PIN intitialization for data port
//////////////////////////////////////////////////////////////////////////////
  {
    // Default initialization
    "AT+CR=0;+CGREG=0;+COLP=0;+CCWA=0;+CSNS=0\r",
    // MC45 initialization
    "AT+CR=0;+CGREG=0;+CCWA=0;+CSNS=0\r",
    // MC46 initialization
    "AT+CR=0;+CGREG=0;+CCWA=0;+CSNS=0\r",
    // MC55 initialization
    NULL,
    // MC56 initialization
    NULL,
    // AC45 initialization
    NULL,
    // MC75 initialization
    NULL,
    // TC63 initialization
    NULL,
  },
//////////////////////////////////////////////////////////////////////////////
// End of post-PIN intitialization for data port
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Post-PIN intitialization for auxilliar data port
//////////////////////////////////////////////////////////////////////////////
  {
    // Default initialization
    "AT+CR=0;+CGREG=0;+COLP=0;+CCWA=0\r",
    // MC45 initialization
    "AT+CR=0;+CGREG=0;+CCWA=0\r",
    // MC46 initialization
    "AT+CR=0;+CGREG=0;+CCWA=0\r",
    // MC55 initialization
    NULL,
    // MC56 initialization
    NULL,
    // AC45 initialization
    NULL,
    // MC75 initialization
    NULL,
    // TC63 initialization
    NULL,
  },
//////////////////////////////////////////////////////////////////////////////
// End of post-PIN intitialization for auxilliar data port
//////////////////////////////////////////////////////////////////////////////
};

